package net.pws.oos.biz.service.impl;

import net.pws.oos.biz.BizException;
import net.pws.oos.biz.model.Area;
import net.pws.oos.biz.query.AreaFinder;
import net.pws.oos.biz.service.AreaService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class DefaultAreaService extends AbstractService implements AreaService {

	@Autowired
	private AreaFinder areaFinder;
	
	@Override
	public Area save(Area area) {
		entityManager.persist(area);
		return area;
	}

	@Override
	public void delete(String id) {
		int count = areaFinder.countCities(id);
		if(count > 0) {
			throw new BizException("此区域有下级区域，不能删除。");
		}
		
		Area area = entityManager.find(Area.class, id);
		if (area == null) {
			return;
		}
		
		area.setDeleted(Boolean.TRUE);
		area.setName(area.getName() + "|已删除");
		save(area);
	}

}
